package com.ruixinzheng.mvidemo.view

import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import com.ruixinzheng.mvidemo.databinding.ActivityMainBinding
import com.ruixinzheng.mvidemo.intent.MainIntent
import com.ruixinzheng.mvidemo.repo.MainRepo
import com.ruixinzheng.mvidemo.state.MainState
import com.ruixinzheng.mvidemo.viewmodel.MainViewModel
import kotlinx.coroutines.launch


class MainActivity : AppCompatActivity() {
    private lateinit var mainViewModel: MainViewModel
    private var adapter = MainAdapter(arrayListOf())
    private lateinit var binding: ActivityMainBinding

    class MyViewModelFactory : ViewModelProvider.Factory {
        override fun <T : ViewModel> create(modelClass: Class<T>): T {
            if (modelClass.isAssignableFrom(MainViewModel::class.java)) {
                return MainViewModel(MainRepo()) as T
            }
            throw IllegalArgumentException("Unknown ViewModel class")
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        setupUI()
        setupViewModel()

    }

    private fun setupUI() {
        var recyclerView = binding.rv
        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.run {
            addItemDecoration(
                DividerItemDecoration(
                    recyclerView.context,
                    (recyclerView.layoutManager as LinearLayoutManager).orientation
                )
            )
        }
        recyclerView.adapter = adapter

        binding.bt.setOnClickListener {
            lifecycleScope.launch {
                mainViewModel.dataIntent.send(MainIntent.FetchData)
            }
        }
    }

    private fun setupViewModel() {
        mainViewModel = ViewModelProvider(
            this,
            MyViewModelFactory()
        ).get(MainViewModel::class.java)

        lifecycleScope.launch {
            mainViewModel.state.collect {
                when (it) {
                    is MainState.Idle -> {
                        binding.rv.visibility = View.INVISIBLE
                        binding.tv.visibility = View.VISIBLE
                        binding.tv.text = "Idle"
                    }
                    is MainState.Loading -> {
                        binding.rv.visibility = View.INVISIBLE
                        binding.tv.visibility = View.VISIBLE
                        binding.tv.text = "Loading..."
                    }
                    is MainState.Data -> {
                        binding.rv.visibility = View.VISIBLE
                        binding.tv.visibility = View.INVISIBLE
                        adapter.setData(it.data)
                    }
                    is MainState.Error -> {
                        binding.rv.visibility = View.INVISIBLE
                        binding.tv.visibility = View.VISIBLE
                        binding.tv.text = "Error"
                    }
                }
            }
        }
    }
}
